<?php
if (!defined('CMS_SYSTEM')) die();
if ($disable_site) {
	echo "<center style=\"margin-top:200px;font-weight:bold\">".$disable_message ."</center>";
}else{
include_once("Cart.class.php");
global $db,$Default_Temp, $time, $currentlang,$prefix;
$cart = new Cart(CART_SESS);
if (isset($_GET['act'])) {
	if ($_GET['act'] == 'add') {
		if (isset($_GET['id'])) {
			$id = intval($_GET['id']);
			$db->sql_query("SELECT title, price FROM {$prefix}_products WHERE id=$id");
			if ($db->sql_numrows() > 0) {
				list($title, $price) = $db->sql_fetchrow();
				$newCart = $cart->getData();
				$found = 0;
				foreach ($newCart as $itemId => $itemDetail) {
					if ($itemId == $id) {
						$newCart[$itemId]['count'] += 1;
						$found = 1;
						break;
					}
				}
				if (!$found) $cart->add($id, $title, $price);
				else $cart->update($newCart);
				$newCart = $cart->getData();
			}
		}
	} elseif ($_GET['act'] == 'update') {
		$count = explode(',', $_POST['count']);
		$list = explode(',', $_POST['list']);
		$name = explode(',', $_POST['name']);
		$price = explode(',', $_POST['price']);
		$newCart = array();
		for ($i = 0; $i < count($list); $i++) {
			$newCart[intval($list[$i])]['count'] = intval(preg_replace('/[^0-9]/','',$count[$i]));
			$newCart[intval($list[$i])]['name'] = $name[$i];
			$newCart[intval($list[$i])]['price'] = floatval($price[$i]);
		}
		$cart->update($newCart);
	}
}
if (!isset($newCart)) $newCart = $cart->getData();
if (isset($_GET['view'])) 
{
	$newCart = $cart->getData();
	$cart_null=count($newCart);
}
include_once("templates/".$Default_Temp."/header.php");
  include_once("templates/".$Default_Temp."/banner.php");
  include_once("templates/".$Default_Temp."/menu.php");
  /*
   * Phan lay ra chi tiet san pham
   * Le duc vien
   */
    $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
	$result = $db->sql_query("SELECT p.title, p.description, p.images,p.price, c.catid, c.title, c.parentid FROM ".$prefix."_products AS p,".$prefix."_products_cat AS c WHERE p.id='$id' AND p.catid=c.catid AND p.alanguage='$currentlang'");
	if(empty($id) || $db->sql_numrows($result) != 1) {
		header("Location: ".url_sid("index.php?f=$module_name")."");
		exit();
	}
	list($title, $description, $images,$price, $catid, $catname, $parentid) = $db->sql_fetchrow($result);
	$header_page_keyword = $description;
	$db->sql_query("UPDATE ".$prefix."_products SET hits=hits+1 WHERE id='$id'");
	if($parentid != 0) {
		$title_cat = page_tilecat($catid, $parentid, $catname);
		$page_title .= "".$title_cat." :: ".$title."";
	} else {
		$title_cat = $catname;
		$page_title .= " :: ".$catname." :: ".$title."";
	}
 echo "<div id=\"wrapper\">";
	 echo "<div class=\"content fix\">";
	     //Noi dung san pham trong gio hang
	     echo "<div class=\"w720 fl\">";
	       echo "<div class=\"common\">";
	            echo "<div class=\"title\">";
				    echo "<div class=\"text\"><a href=\"\"><span>"._SHOPPING_CART."</span></a></div>";
				echo "</div>";
			    echo "<div class=\"wrap fix\">";
			       //phan lay danh sach san pham trong gio hang
			       echo "<div class=\"yourCart\">";
								echo '<form method="POST" action="'.url_sid("index.php?f=$module_name&do=checkout")."\" id=\"orderForm\">";
									echo "<table class=\"tblCart\" cellpadding=\"0\" cellspacing=\"0\">";
										echo "<tr>";
											echo "<td>"._PRODUCT_NO."</td>";
											echo "<th>"._PRODUCT_NAME."</th>";
											echo "<th>"._PRODUCT_COUNT."</th>";
											echo "<th>"._PRODUCT_PRICE."</th>";
											echo "<th>"._PRODUCT_CART_PRICE."</th>";
										echo "</tr>";
										$productList = array();
										$productName = array();
										$productPrice = array();
										$request = '';
										$i = 1;
										$total = 0;
										foreach ($newCart as $productId => $productDetail) {
											echo "<tr>";
												echo "<td>$i</td>";
												echo "<td><a href=\"\">{$productDetail['name']}</a></td>";
												echo "<td><input type=\"text\" id=\"product_{$productId}_count\" name=\"product_{$productId}_count\"  value=\"{$productDetail['count']}\" class=\"inputText w70\"/></td>";
												echo "<td>".number_format((int) $productDetail['price'])."</td>";
												$currentTotal = (int) $productDetail['price'] * (int) $productDetail['count'];
												
												echo "<td>".number_format($currentTotal)."</td>";
											echo "</tr>";
											$productList[] = $productId;
											$productName[] = $productDetail['name'];
											$productPrice[] = $productDetail['price'];
											$total += $currentTotal;
											$i++;
										}
										$productList = implode(',', $productList);
										$productName = implode(',', $productName);
										$productPrice = implode(',', $productPrice);
										echo "<tr>";
											echo "<th colspan=\"4\">"._PRODUCT_TOTAL."</th>";
											echo "<td>".bsVndDot($total)."</td>";
										echo "</tr>";
									echo "</table>";
									echo "<div class=\"payment fix\">";
										echo "<span class=\"f1\">“"._PRODUCT_UNIT."”</span>";
										echo "<div class=\"fr\">";
											echo "<a href=\"\"><img src=\"templates/".$Default_Temp."/images/payment_nganluong.jpg\"/></a>";
											echo "<a href=\"\"><img src=\"templates/".$Default_Temp."/images/payment_baokim.jpg\"/></a>";
										echo "</div>";
									echo "</div>";
									echo "<div class=\"buyer\">";
										echo "<div class=\"info_order\">"._CART_ORDER_INFO."</div>";
										echo "<div class=\"line fix\">";
											echo "<div class=\"w120 fl\">";
												echo "<label for=\"person\">"._PRODUCT_FULLNAME.":</label>";
											echo "</div>";
											echo "<div class=\"fl w520\">";
												echo "<input type=\"text\" class=\"inputForm w250\" name=\"person\" id=\"person\" maxlength=\"150\"/><span class=\"field_notice\">*</span>";
											echo "</div>";
										echo "</div>";
										echo "<div class=\"line fix\">";
											echo "<div class=\"w120 fl\">";
												echo "<label for=\"address\">"._PRODUCT_ADDRESS.":</label>";
											echo "</div>";
											echo "<div class=\"fl w520\">";
												echo "<input type=\"text\" class=\"inputForm w250\" name=\"address\" maxlength=\"150\" id=\"address\"/><span class=\"field_notice\">*</span>";
											echo "</div>";
										echo "</div>";
										echo "<div class=\"line fix\">";
											echo "<div class=\"w120 fl\">";
												echo "<label for=\"phone\">"._PRODUCT_PHONE.":</label>";
											echo "</div>";
											echo "<div class=\"fl w520\">";
												echo "<input type=\"text\" class=\"inputForm w250\" name=\"phone\" maxlength=\"12\" id=\"phone\"/><span class=\"field_notice\">*</span>";
											echo "</div>";
										echo "</div>";
										echo "<div class=\"line fix\">";
											echo "<div class=\"w120 fl\">";
												echo "<label for=\"email\">"._PRODUCT_EMAIL.":</label>";
											echo "</div>";
											echo "<div class=\"fl w520\">";
												echo "<input type=\"text\" class=\"inputForm w250\" name=\"email\" id=\"email\"/><span class=\"field_notice\">*</span>";
											echo "</div>";
										echo "</div>";
										echo "<div class=\"line fix\">";
											echo "<div class=\"w120 fl\">";
												echo "<label for=\"content\">"._PRODUCT_MESSAGE.":</label>";
											echo "</div>";
											echo "<div class=\"fl w520\">";
												echo "<textarea class=\"inputForm h120 w500\" name=\"content\" maxlength=\"1000\" id=\"content\"></textarea><span class=\"field_notice\">*</span>";
											echo "</div>";
										echo "</div>";
									echo "</div>";
									echo "<div class=\"tc\">";
										echo "<input type=\"submit\" name=\"submit\" value=\""._PRODUCT_CHECKOUT."\" class=\"btnOrder\"/>";
										echo "<input type=\"button\" name=\"submit2\" value=\""._PRODUCT_CART_UPDATE."\" class=\"btnEdit\" onclick=\"prepareToPost('$productList', '$productName', '$productPrice');\"\/>";
									echo "</div>";
								echo "</form>";
						echo "</div>";
			       //ket thuc phan lay thong tin
			    echo "</div>";
	       echo "</div>";
	     echo "</div>";
	     include_once("templates/".$Default_Temp."/rightbody.php");
	 echo "</div>";
	 themeSlideShow(8);
	 themeFooter();
 echo "</div>";
 /*
  * Ket thuc phan san pham
  * 
  */
include_once("templates/".$Default_Temp."/footer.php");
echo "<script type=\"text/javascript\">";
		echo "$(function(){";
			echo "$('#orderForm').validate({";
				echo "errorPlacement: function(error, element){";
					echo "$(element).next('.field_notice').hide();";
					echo "$(element).after(error);";
				echo "},";
				echo "success       : function(label){";
					echo "label.addClass('right').text('OK!');";
				echo "},";
				echo "rules : {";
					echo "person : {";
						echo "required   : true,";
						echo "maxlength: 150";
					echo "},";
					echo "address : {";
						echo "required   : true,";
						echo "maxlength: 150";
					echo "},";
					echo "phone : {";
						echo "required   : true,";
						echo "number: true,";
						echo "maxlength: 12,";
						echo "minlength: 8";
					echo "},";
					echo "email : {";
						echo "required   : true,";
						echo "email: true";
					echo "},";
					echo "content : {";
						echo "maxlength: 1000";
					echo "}";
				echo "},";
				echo "messages : {";
					echo "person : {";
						echo "required   : \""._NAME_NULL."\",";
						echo "maxlength: \""._NAME_MAX."\"";
					echo "},";
					echo "address : {";
						echo "required   : \""._ADDRESS_NULL."\",";
						echo "maxlength: \""._ADDRESS_MAX."\"";
					echo "},";
					echo "phone : {";
						echo "required   : \""._PHONE_NULL."\",";
						echo "number: \""._PHONE_NUMBER."\",";
						echo "maxlength: \""._PHONE_MAX."\",";
						echo "minlength: \""._PHONE_MIN."\"";
					echo "},";
					echo "email : {";
						echo "required   : \""._EMAIL_NULL."\",";
						echo "email: \""._EMAIL_ERROR."\"";
					echo "},";
					echo "content : {";
						echo "maxlength: \""._CONTENT_MAX."\"";
					echo "}";
				echo "},";
			echo "});";
		echo "});";
	echo "</script>";
	echo "<script type=\"text/javascript\" src=\"templates/".$Default_Temp."/js/validate.js\"></script><!--Dùng cho trang giỏ hàng-->	";
}
?>